package com.zhongx.xingcd.zxxcd.controller;

import com.zhongx.xingcd.zxxcd.common.Isphonenum;
import com.zhongx.xingcd.zxxcd.service.UserLoginService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Fan
 * <p>
 * Created on 2018/11/7 9:29 PM
 * Description : 控制登录和前后台主页跳转
 */
@Controller
public class LoginController {

    @Autowired
    private UserLoginService service;

    @RequestMapping(value = "/login",method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String logout(@RequestBody JSONObject jsonParam, HttpSession session, HttpServletResponse response) {

        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(formatter.format(calendar.getTime()));
        List list = new ArrayList();
        Map map = new HashMap();
        map.put("username",jsonParam.get("user_name"));
        map.put("password",jsonParam.get("user_passwd"));
        list = service.getUserLoginMes(map);
        JSONObject jsonObject = new JSONObject();
        if(list.isEmpty()){
            jsonObject.put("result_data",list);
            jsonObject.put("result_code",1);
            jsonObject.put("result_mes","用户或密码错误");
        }else {
            session.setAttribute("username", jsonParam.get("user_name"));
            int a =service.userLoginTime((String) jsonParam.get("user_name"),formatter.format(calendar.getTime()));
            jsonObject.put("result_data",list);
            jsonObject.put("result_code",0);
            jsonObject.put("result_mes","登录成功");
        }

        return jsonObject.toString();
    }


    @RequestMapping("/test")
    public String test() {
        return "zxxcd/index";
    }

    /**
     * 用户注册验证
     * @param jsonParam
     * @return
     */
    @RequestMapping(value = "/register",method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String register(@RequestBody JSONObject jsonParam, HttpSession session,HttpServletResponse response) {
        session.setAttribute("username", jsonParam.get("user_name"));
        //System.out.println("接收的数据 "+jsonParam);
        //List list = new ArrayList();
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(formatter.format(calendar.getTime()));
        JSONObject jsonObject = new JSONObject();
        if(this.isMobileNO((String)jsonParam.get("phone"))&&this.check2((String)jsonParam.get("QQNum"))){
            Map map = new HashMap();
            map.put("username",jsonParam.get("user_name"));
            map.put("password",jsonParam.get("user_passwd"));
            map.put("phonenumber",jsonParam.get("phone"));
            map.put("qqnumber",jsonParam.get("QQNum"));
            map.put("userid", Isphonenum.getUUID());
            map.put("registration_time", formatter.format(calendar.getTime()));
            map.put("user_jurisdiction", 0);
            int namecount = service.getUserNumByName((String)jsonParam.get("user_name"));
            if(namecount==0){
                int addUser = service.addUserInfo(map);
                System.out.println("添加用户数量 "+addUser);
                if(addUser==1){
                    jsonObject.put("result_data",jsonParam.get("user_name"));
                    jsonObject.put("result_code",0);
                    jsonObject.put("result_mes","用户注册成功");
                }else {
                    jsonObject.put("result_data",addUser);
                    jsonObject.put("result_code",1);
                    jsonObject.put("result_mes","用户注册失败");
                }
            }else {
                jsonObject.put("result_data",namecount);
                jsonObject.put("result_code",1);
                jsonObject.put("result_mes","用户名重复");
            }
        }else{
            jsonObject.put("result_data","手机号或者qq号错误");
            jsonObject.put("result_code",1);
            jsonObject.put("result_mes","用户注册失败");
        }




        return jsonObject.toString();
    }

    /**
     * 用户名注册验证
     * @param jsonParam
     * @return
     */
    @RequestMapping(value = "/registerusername",method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String registerusername(@RequestBody JSONObject jsonParam, HttpServletResponse response) {
        //System.out.println("接收的数据 "+jsonParam);

        int namecount = service.getUserNumByName((String)jsonParam.get("user_name"));

        JSONObject jsonObject = new JSONObject();
        if(namecount==0){
            jsonObject.put("result_data",namecount);
            jsonObject.put("result_code",0);
            jsonObject.put("result_mes","用户名可用");
        }else {
            jsonObject.put("result_data",namecount);
            jsonObject.put("result_code",1);
            jsonObject.put("result_mes","用户名重复");
        }

        return jsonObject.toString();
    }

    //登陆成功
    @RequestMapping("/goHomePage")
    public String goHomePage(HttpSession session) {
        String myusername = (String) session.getAttribute("username");
        String userJeon = service.getUserJurisdiByName(myusername);
        if("1".equals(userJeon)){
            return "zxxcd/admin/admin_user_manage";
        }
        return "zxxcd/homePage";
    }

//文件上传
    @RequestMapping("/fileupdown")
    public String fileupdown() {
        return "zxxcd/biao";
    }


    /**
     * 登出
     * @param session
     * @return
     */
    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        //session.invalidate();
       // System.out.println("接收的数据 "+session.getAttribute("username"));
        session.removeAttribute("username");
       // System.out.println("接收的数据 "+session.getAttribute("username"));
       // System.out.println("登出id的数据 "+session.getId());
        return "zxxcd/index";
    }


    public boolean isMobileNO(String mobiles) {
        //let mobilereg1 = /^1[3|5|6|7|8|9][0-9]{9}$/; // 1[356789][0-9] （后面再接8位数）
        //let mobilereg2 = /^14[5|7][0-9]{8}$/; // 14[57] （后面再接8位数）
        Pattern p = Pattern.compile("^1[3|5|6|7|8|9][0-9]{9}$");
        Matcher m = p.matcher(mobiles);
        Pattern p1 = Pattern.compile("^14[5|7][0-9]{8}$");
        Matcher m1 = p1.matcher(mobiles);
        if(m.matches()||m1.matches()){
            return true;
        }
        return false;
    }
    public boolean check2(String qq) {
        return qq.matches("[1-9][0-9]{4,14}");
    }


    @RequestMapping(value = "/getUserInfoByAdmin",method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String getUserInfoByAdmin(@RequestBody JSONObject jsonParam, HttpSession session, HttpServletResponse response) {

        List list = new ArrayList();
        Map map = new HashMap();
        map.put("username",jsonParam.get("user_name"));

        list = service.getUserMesByAdmin(map);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result_data",list);
        jsonObject.put("result_code",0);
        jsonObject.put("result_mes","用户信息");
        return jsonObject.toString();
    }
}

